package com.banciyuan.bcywebview.biz.goods.store;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;

/**
 * Created by squall on 2016/11/3.
 */

public class AllGoodsAdapter extends BaseAdapter {

    private List<DetailGoods> dlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;

    public AllGoodsAdapter(List<DetailGoods> dlist, Context mContext) {
        this.dlist = dlist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return dlist.size() % 2 == 0 ? dlist.size() / 2 : dlist.size() / 2 + 1;
    }

    @Override
    public Object getItem(int position) {
        return dlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {

        public ImageView[] pics;
        public TextView[] titles;
        public TextView[] names;
        public TextView[] prices;

        public ViewHolder(View convertView) {
            pics = new ImageView[]{(ImageView) convertView.findViewById(R.id.goods_pic_one), (ImageView) convertView.findViewById(R.id.goods_pic_two)};
            titles = new TextView[]{(TextView) convertView.findViewById(R.id.goods_title_one), (TextView) convertView.findViewById(R.id.goods_title_two)};
            names = new TextView[]{(TextView) convertView.findViewById(R.id.goods_user_one), (TextView) convertView.findViewById(R.id.goods_user_two)};
            prices = new TextView[]{(TextView) convertView.findViewById(R.id.goods_price_one), (TextView) convertView.findViewById(R.id.goods_price_two)};
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.goods_store_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int index = position * 2;
        List<DetailGoods> templist = new ArrayList<>();
        DetailGoods detailGoods1 = dlist.get(index);
        templist.add(detailGoods1);
        DetailGoods detailGoods2 = null;
        if (index + 1 < dlist.size()) {
            detailGoods2 = dlist.get(index + 1);
            templist.add(detailGoods2);
            ((View) (viewHolder.prices[1].getParent())).setVisibility(View.VISIBLE);
        } else {
            ((View) (viewHolder.prices[1].getParent())).setVisibility(View.INVISIBLE);
        }
        for (int i = 0; i < templist.size(); i++) {
            final DetailGoods tempD = templist.get(i);
            if (!TextUtils.isEmpty(tempD.getCover())) {
                ImageLoader.getInstance().displayImage(tempD.getCover(), viewHolder.pics[i], ImageOptions.options);
            }
            if (!TextUtils.isEmpty(tempD.getTitle())) {
                viewHolder.titles[i].setText(tempD.getTitle());
            }
            if (!TextUtils.isEmpty(tempD.getUname())) {
                viewHolder.names[i].setText(String.format(mContext.getString(R.string.power_by), tempD.getUname()));
            }
            if (!TextUtils.isEmpty(tempD.getBasic_price())) {
                viewHolder.prices[i].setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(tempD.getBasic_price())));
            }
            ((View) viewHolder.pics[i].getParent()).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, tempD.getZg_id());
                }
            });
        }
        return convertView;
    }
}
